<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class AdminRole extends Base
{
    //角色添加
    public function roleAdd($data=array()){
        try {
            //添加角色数据
            if(!empty($data)){
                $temp = explode(',',$data['powers']);
                unset($data['powers']);
                $res = $this->firstOrCreate($data);
                $powers = [];
                foreach ($temp as $pid){
                    $powers[]=['roleid'=>$res->id,'pid'=>$pid];
                }
            }else{
                $res = $this->save();
                $powers = [];
            }
            //添加角色权限
            $model = new RolePower();
            $result = $model->rolePowerAdd($powers);
            if($res){ //角色添加成功
                return $result;
            }else{ //角色添加失败
                return [
                    'code'=>20002,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //关联权限表
    public function powers(){
        return  $this->hasMany('App\Models\RolePower','roleid','id');
    }

    //获取角色列表
    /**
     * @param array $options  分页参数，含limit(每页数据条数)和page（当前页）
     * @param array $where  分页条件
     */
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = DB::table('admin_roles')->where($where);
            if(!empty($whereDates)){
                $model = $model->whereDate('created_at','>=',$whereDates[0])->whereDate('created_at','<',$whereDates[1]);
            }
            $model = $model->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    /**
     * 编辑权限
     */
    public function doUpdate($data=array()){
        try {
            //存储数据
            $model = self::find($data['id']);
            $model->rolename = $data['rolename'];
            $res = $model->save();
            $powers = explode(',',$data['powers']);
            $model = new RolePower();
            //删除权限中间表中原有数据
            $del=$model->where('roleid','=',$data['id'])->delete();
            $insertData = [];
            foreach ($powers as $p){
                $insertData[] = ['roleid'=>$data['id'],'pid'=>$p];
            }
            //权限中间表添加数据
            $ins = $model->insert($insertData);
            if($res && $ins && $del){
                return [
                    'code'=>0,
                    'data'=>[]
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }
}
